package com.yhxd.modular.reference.service;

import com.yhxd.modular.reference.dao.DutiesDao;
import com.yhxd.modular.reference.entity.Duties;
import com.yhxd.modular.system.entity.User;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class DutiesService {

    @Autowired
    private DutiesDao dutiesDao;

    public Duties findById(Long id) {
        return this.dutiesDao.findById(id).orElse(new Duties());
    }

    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdate(Duties duties) {
        User shiroUser = (User) SecurityUtils.getSubject().getPrincipals().getPrimaryPrincipal();
        Duties oldDuties = this.dutiesDao.findById(duties.getId()).orElse(null);
        if (null == oldDuties) {
            duties.setCreateName(shiroUser.getRealName());
            duties.setUpdateName(shiroUser.getRealName());
            this.dutiesDao.save(duties);
        } else {
            oldDuties.setContent(duties.getContent());
            oldDuties.setUpdateName(shiroUser.getRealName());
            this.dutiesDao.save(oldDuties);
        }
    }
}
